<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/2 0002
 * Time: 11:41
 */

namespace app\admin\controller;


use think\Db;

class Order extends Middle
{
    public function Change(){
        $page = $this->page($this->data);
        $list = Db::name('order')
            ->alias('a')
            ->join('gear b','a.id=b.id')
            ->join('user c','a.uid=c.uid')
            ->field('a.time,a.status,a.pay_time,a.number,b.price,b.desc,c.phone as nickname')
            ->order('time desc')
            ->limit($page['page'],$page['limit'])
            ->select();
        $count = Db::name('order')
            ->alias('a')
            ->join('gear b','a.id=b.id')
            ->join('user c','a.uid=c.uid')
            ->count();
        return $this->returnSuccess(['list'=>$list,'count'=>$count]);
    }

    public function Cash(){
        $page = $this->page($this->data);
        $list = Db::name('cash')
            ->alias('a')
            ->join('user b','a.uid=b.uid')
            ->order('status asc')
            ->field('a.price,a.status,a.create_time,a.addr,a.remarks,b.phone as nickname,a.id,a.num,a.desc')
          	->order('a.create_time desc,a.status asc')
            ->limit($page['page'],$page['limit'])
            ->select();      
        $count = Db::name('cash')
            ->alias('a')
            ->join('user b','a.uid=b.uid')
            ->count();
        return $this->returnSuccess(['list'=>$list,'count'=>$count]);
    }


    public function CashAuth(){
        $id = isset($this->data['id']) ? $this->data['id'] : 0;
        $map = ['id'=>$id];
        $res = Db::name('cash')->where($map)->find();
        if(!$res)return $this->returnError('参数错误');
        if($res['status'] != 0)return $this->returnError('状态错误');
        $map['status'] = 1;
        $ref = Db::name('cash')->update($map);
        if($ref)return $this->returnSuccess(null,'审核通过');
        return $this->returnError($this->Error);
    }

    public function CashFalse(){
        $id = isset($this->data['id']) ? $this->data['id'] : 0;
        $map = ['id'=>$id];
        $res = Db::name('cash')->where($map)->find();
        if(!$res)return $this->returnError('参数错误');
        if($res['status'] != 0)return $this->returnError('状态错误');
        $map['status'] = 2;
        Db::startTrans();
        try{
            Db::name('cash')->update($map);
            Db::name('user')->where(['uid'=>$res['uid']])->setInc('integral',$res['price']);
            $this->IntegralLog($res['uid'],'兑换数字资产审核失败,退回积分',1,$res['price']);
            Db::commit();
            return $this->returnSuccess(null,'操作成功');
        }catch (\Exception $e){
            Db::rollback();
            return $this->returnError($this->Error);
        }

    }



}